package structure.stack;

/**
 * @Author wangj
 * @Date 2022/4/22 21:41
 * 模拟计算器栈
 */
public class CalculatorStack {

    /**栈的最大容量*/
    private int maxSize;
    private int[] stack;
    private int top = -1;

    public CalculatorStack(int maxSize){
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    public boolean isFull(){
        return top == maxSize - 1;
    }

    public boolean isEmpty(){
        return top == -1;
    }

    public void show(){
        if(isEmpty()){
            System.out.println("栈空");
            return;
        }
        for (int i = top; i >=0 ;i--){
            System.out.printf("stack[%d]=%d",i,stack[i]);
        }
    }

    public void push(int value){
        if(isFull()){
            System.out.println("栈满");
            return;
        }
        top++;
        stack[top] = value;
    }

    public int pop(){
        if(isEmpty()){
            System.out.println("栈空");
            return -1;
        }
        int result = stack[top];
        top--;
        return result;
    }

    public boolean isOperation(char ch){
        return ch == '+' || ch == '-' || ch == '*' || ch == '/';
    }

    public int peek(){
        return stack[top];
    }

    public int priority(int operation){
        if(operation == '*' || operation == '|'){
            return 1;
        }else if(operation == '+' || operation == '-'){
            return 0;
        }else {
            throw new RuntimeException("操作符非法");
        }
    }

    public int calculator(int perNum, int afterNum, char operation){
        int result = 0;
        switch (operation){
            case '+':
                result = perNum + afterNum;
                break;
            case '-':
                result = afterNum - perNum;
                break;
            case '*':
                result = perNum * afterNum;
                break;
            case '/':
                result = afterNum / perNum;
                break;
        }
        return result;
    }
}
